<?php
/*
 * variable/load-index.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;

// Variables
$employes	 = $this->employes;
$coef		 = 1.54;
$post		 = $this->post;

// Table Planning Semaine
$planningSemaineTable = new RHX\Model\Table\PlanningSemaineTable($this->adapter);

// traitement
$countEmploye = count($employes);
if ($countEmploye == 0) {
    $employes = array();
    foreach ($this->employeList as $e) {
	$data			 = array();
	$data['matricule']	 = $e->matricule;
	$data['nom']		 = $e->nom1;
	$data['prenom']		 = $e->prenom;
	$data['contrat_debut']	 = $e->contrat_debut;
	$poste			 = $e->getPoste();
	$data['poste']		 = $poste;
	$data['code']		 = $poste->code;
	$data['semaines']	 = array();
	// Contrat Horaire
	$avenant		 = $e->getAvenant();
	$data['hebdo']		 = $avenant->hebdo;
	$data['coef']		 = $e->getCoefficient();
	$data['object']		 = $e;

	$employes[] = $data;
    }
}
?>
<div class='rhx-list-container'>
    <table class="table table-striped list-table">
	<thead>
	    <tr>
		<td colspan="6">Variable de Paie</td>
		<td colspan="3">Entrée / Sortie sur Mois</td>
		<td colspan="6">Durée Hebdomadaire</td>
		<td colspan="5">Informations</td>
		<td colspan="3">Compteur</td>
		<td colspan="5">Contrat Hebdomadaire</td>
		<td rowspan="2">Coef</td>
		<!--td colspan="5">Taux Horaire Chargé</td-->
		<td colspan="2">CP Restant</td>
	    </tr>
	    <tr>
		<td>Matricule</td>
		<td>Nom</td>
		<td>Prénom</td>
		<td>Date d'Entrée</td>
		<td>Poste</td>
		<td>Contrat Horaire</td>
		<td>Entrée sur le mois</td>
		<td>Sortie sur le mois</td>
		<td>Motif</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="1">S1</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="2">S2</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="3">S3</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="4">S4</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="5">S5</a>
		</td>
		<td>Moyenne</td>
		<td>CSS</td>
		<td>AM1</td>
		<td>AM2</td>
		<td>AT</td>
		<td>ABS</td>
		<td>Compteur Retard</td>
		<td>Compteur Heures Planifiées</td>
		<td>Résultat</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="1">S1</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="2">S2</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="3">S3</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="4">S4</a>
		</td>
		<td>
		    <a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="5">S5</a>
		</td>
		<!--td></td-->
		<!--td>S1</td>
		<td>S2</td>
		<td>S3</td>
		<td>S4</td>
		<td>S5</td-->
		<td>N</td>
		<td>N - 1</td>
	    </tr>
	</thead>
	<tbody>
	    <?php
	    $jaune1		 = '#FFF67F';
	    $jaune2		 = '#FFEE05';
	    $odd		 = 1;
	    $colorJaune	 = '';
	    ?>
	    <?php foreach ($employes as $e): ?>
		<?php $colorJaune	 = (($odd++ % 2) == 0) ? $jaune2 : $jaune1; ?>
		<?php
		$contratDebut	 = new RHDateTime($e['contrat_debut']);
		?>
    	    <tr>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo $e['matricule']; ?></td>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo $e['nom']; ?></td>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo $e['prenom']; ?></td>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo $contratDebut->date('d/m/Y'); ?></td>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo $e['code']; ?></td>
    		<td style='background: <?php echo $colorJaune; ?>;'><?php echo RHDateTime::toTime($e['hebdo']); ?></td>
    		<td></td>
    		<td></td>
    		<td></td>
    		<!-- DUREE HEBDOMADAIRE -->
		    <?php $sem		 = $e['semaines']; ?>
		    <?php
		    $total		 = 0.0;
		    $divide		 = 0;
		    $retard		 = 0;
		    $retardHeure	 = new RHDateTime();
		    $retardHeure->setTime();
		    $hebdoTotal	 = 0;
		    ?>
		    <?php for ($i = 0; $i < 5; $i++): ?>
			<?php
			$s = array(
			    'libelle'		 => 'S' . ($i + 1),
			    'lundi'			 => '0000-00-00',
			    'dimanche'		 => '0000-00-00',
			    'heure_travaille'	 => 0,
			    'heure_non_plannifie'	 => 0,
			    'hebdo'			 => 0,
			    'taux'			 => 0,
			    'retard'		 => 0,
			    'retard_heure'		 => '00:00',
			);
			if (isset($sem[$i])) {
			    $s = $sem[$i];
			    $divide++;
			}
			$travaille	 = floatval($s['heure_travaille']);
			$total += $travaille;
			// total heure hebdo
			$hebdoTotal += floatval($s['hebdo']);
			$bold		 = '';
			if ($travaille > 0)
			    $bold		 = ' style="font-weight: bold;"';
			$retard += $s['retard'];
			$retardHeure->addHours($s['retard_heure'], false);
			?>
			<td<?php echo $bold; ?>><?php echo number_format($travaille, 2, ',', ' '); ?></td>
		    <?php endfor; ?>
		    <?php
		    $moyenne	 = 0.0;
		    if ($divide != 0)
			$moyenne	 = $total / $divide;
		    $affMoyenne	 = number_format($moyenne, 2, ',', ' ');
		    $bold		 = '';
		    if ($affMoyenne > 0)
			$bold		 = ' font-weight: bold';
		    ?>
    		<td style='background: <?php echo $colorJaune; ?>;<?php echo $bold; ?>'><?php echo $affMoyenne; ?></td>
    		<!-- INFORMATIONS -->
		    <?php
		    // Occurence pour CSS
		    $css		 = $planningSemaineTable->getOccurencePeriode(XRules::CongeSansSolde, $e['matricule'], $post->periode);
		    $am1		 = $planningSemaineTable->getOccurencePeriode(XRules::ArretMaladie1, $e['matricule'], $post->periode);
		    $am2		 = $planningSemaineTable->getOccurencePeriode(XRules::ArretMaladie2, $e['matricule'], $post->periode);
		    $at		 = $planningSemaineTable->getOccurencePeriode(XRules::AccidentTravail, $e['matricule'], $post->periode);
		    $abs		 = $planningSemaineTable->getOccurencePeriode(XRules::Absent, $e['matricule'], $post->periode);
		    ?>
    		<td><?php echo $css; ?></td>
    		<td><?php echo $am1; ?></td>
    		<td><?php echo $am2; ?></td>
    		<td><?php echo $at; ?></td>
    		<td><?php echo $abs; ?></td>
    		<!-- COMPTEUR -->
    		<td><?php echo $retardHeure->totalHours() . ' (' . $retard . ')'; ?></td>
    		<td><?php echo number_format($total, 2, ',', ' '); ?></td>
    		<td>
    		    <b><?php
			    $resultat	 = ($total + RHDateTime::toFloat($retardHeure->totalHours())) - $hebdoTotal;
			    echo ($resultat > 0) ? '+' . number_format($resultat, 2, ',', ' ') : number_format($resultat, 2, ',', ' ');
			    ?></b>
    		</td>
    		<!-- TAUX HORAIRE -->
		    <?php for ($i = 0; $i < 5; $i++): ?>
			<?php
			$s = array(
			    'libelle'		 => 'S' . ($i + 1),
			    'lundi'			 => '0000-00-00',
			    'dimanche'		 => '0000-00-00',
			    'heure_travaille'	 => 0,
			    'heure_non_plannifie'	 => 0,
			    'hebdo'			 => 0,
			    'taux'			 => 0,
			);
			if (isset($sem[$i])) {
			    $s = $sem[$i];
			}
			$hebdo	 = floatval($s['hebdo']);
			$bold	 = '';
			if ($hebdo > 0)
			    $bold	 = ' style="font-weight: bold;"';
			?>
			<td<?php echo $bold; ?>><?php echo number_format($hebdo, 2, ',', ' '); ?></td>
		    <?php endfor; ?>
    		<!-- COEFF -->
    		<td style='background: <?php echo $colorJaune; ?>; font-weight: bolder;'><?php echo number_format($e['coef'], 2, ',', ' '); ?></td>
    		<!-- TAUX CHARGE -->
		    <?php for ($i = 0; $i < 5; $i++): ?>
			<?php
			$s = array(
			    'libelle'		 => 'S' . ($i + 1),
			    'lundi'			 => '0000-00-00',
			    'dimanche'		 => '0000-00-00',
			    'heure_travaille'	 => 0,
			    'heure_non_plannifie'	 => 0,
			    'hebdo'			 => 0,
			    'taux'			 => 0,
			);
			if (isset($sem[$i])) {
			    $s = $sem[$i];
			}
			$travaille	 = floatval($s['heure_travaille']);
			$charge		 = $travaille * $e['coef'];
			$bold		 = '';
			if ($charge > 0)
			    $bold		 = ' style="font-weight: bold;"';
			?>
				<!--td<?php echo $bold; ?>><?php echo number_format($charge, 2, ',', ' '); ?></td-->
		    <?php endfor; ?>
    		<td></td>
    		<td></td>
    	    </tr>
	    <?php endforeach; ?>
	</tbody>
	<tfoot>
	    <tr>
		<td colspan="6">Variable de Paie du Personnel Entrant</td>
		<td colspan="3">Entrée / Sortie sur Mois</td>
		<td colspan="6">Durée Hebdomadaire</td>
		<td colspan="5">Informations</td>
		<td colspan="3">Compteur</td>
		<td colspan="5">Contrat Hebdomadaire</td>
		<td rowspan="2">Coef</td>
		<!--td colspan="5">Taux Horaire Chargé</td-->
		<td colspan="2">CP Restant</td>
	    </tr>
	    <tr>
		<td>Matricule</td>
		<td>Nom</td>
		<td>Prénom</td>
		<td>Date d'Entrée</td>
		<td>Poste</td>
		<td>Contrat Horaire</td>
		<td>Entrée sur le mois</td>
		<td>Sortie sur le mois</td>
		<td>Motif</td>
		<td>S1</td>
		<td>S2</td>
		<td>S3</td>
		<td>S4</td>
		<td>S5</td>
		<td>Moyenne</td>
		<td>CSS</td>
		<td>AM1</td>
		<td>AM2</td>
		<td>AT</td>
		<td>ABS</td>
		<td>Compteur de Retard</td>
		<td>Compteur Heures Plannifiées</td>
		<td>Résultat</td>
		<td>S1</td>
		<td>S2</td>
		<td>S3</td>
		<td>S4</td>
		<td>S5</td>
		<!--td></td-->
		<!--td>S1</td>
		<td>S2</td>
		<td>S3</td>
		<td>S4</td>
		<td>S5</td-->
		<td>N</td>
		<td>N - 1</td>
	    </tr>
	    <tr>
		<?php $colorJaune	 = ($colorJaune == $jaune1) ? $jaune2 : $jaune1; ?>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<!--td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td-->
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
	    </tr>
	    <tr>
		<?php $colorJaune	 = ($colorJaune == $jaune1) ? $jaune2 : $jaune1; ?>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<!--td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td-->
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
	    </tr>
	    <tr>
		<?php $colorJaune	 = ($colorJaune == $jaune1) ? $jaune2 : $jaune1; ?>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<!--td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td-->
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
		<td style='background: <?php echo $colorJaune; ?>'></td>
	    </tr>
	</tfoot>
    </table>
</div>

<script type="text/javascript">
// Démarrage
    $(function() {
	// Affichage des plannings en consultation
	$('.show-planning').click(function(e) {
	    e.preventDefault();
	    semaine = $(this).attr('data-semaine');
	    periode = $(this).attr('data-periode');
	    site = $(this).attr('data-site');
	    // Récupération de la date de la semaine
	    ajaxS = $.ajax({
		dataType: 'json',
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller' => 'variable', 'action' => 'getSemaine')) ?>',
		data: '&periode=' + periode + '&semaine=' + semaine
	    });
	    ajaxS.success(function(data) {
		// Récupération du template de création du site
		ajaxT = $.ajax({
		    dataType: 'json',
		    type: 'post',
		    url: '<?php echo $this->url('RHX/default', array('controller' => 'variable', 'action' => 'getTemplate')) ?>',
		    data: '&semaine=' + data['semaine'].id + '&site=' + site
		});
		ajaxT.success(function(result) {
		    // Chargement du planning Semaine
		    dataPlanning = '&data-lundi=' + data['semaine'].lundi + '&template=' + result.template + '&site_id=' + site + '&editable=0&lundi=on&mardi=on&mercredi=on&jeudi=on&vendredi=on&debut=0000-00-00&fin=0000-00-00&dialog=1';
		    ajaxP = $.ajax({
			type: 'post',
			url: '<?php echo $this->url('RHX/default', array('controller' => 'planning', 'action' => 'edit')) ?>',
			data: dataPlanning
		    });
		    ajaxP.success(function(planning) {
			width = $(document).width() - 20;
			height = $(document).height() - 20;
			$("#dialog1").hide().html(planning).fadeIn('normal').dialog({
			    width: width,
			    height: height,
			    title: "Planning Semaine du " + data['semaine'].lundi + " au " + data['semaine'].dimanche,
			    autoOpen: true,
			    show: {effect: 'fade', duration: 1000},
			    hide: {effect: 'fade', duration: 700}
			});
		    });
		    ajaxP.error(function() {
		    });
		});
		ajaxT.error(function() {
		});
	    });
	    ajaxS.error(function() {
		//
	    });
	});
    });
</script>


<style type='text/css'>
    .rhx-list-container{
	font-size: 0.9em;
    }
    .rhx-list-container table{
	vertical-align: middle;
    }
    .rhx-list-container table thead{
	font-size: 0.9em;
    }
</style>